class Ticket < ActiveRecord::Base
  acts_as_taggable
  
  has_many :ticket_replies
  belongs_to :project
  
  belongs_to :user
  belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id'
  belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_id'
  
  validates_presence_of     :name
  validates_length_of       :name,    :within => 4..100
  
  def self.states
    default_states
  end
  
  def self.priorities
    default_priorities
  end
  
  def self.types
    default_types
  end
  
  def time_spend
      count = 0
      for reply in self.ticket_replies 
        count += reply.hours
      end
      return count
  end
  
  private
  
  def self.default_states
    {
      'unassigned' => 1,
      'in_treatment' => 11,
      'finished' => 21,
      'unrepairable' =>26,
      'wont_fix' => 31,
      'duplicate' => 33,
      'approved' => 41,
      'not_approved' => 51
    }
  end
  
  def self.default_priorities
    {
      'low' => -11,
      'unassigned' => 0,
      'normal' => 1,
      'medium' => 11,
      'high' => 21,
    }
  end
  
  def self.list_types
    {
      'Unassigned'    => 'unassigned',
      'In Treatment'  => 'in_treatment',
      'Finished'      => 'finished',
      'Unrepairable'  => 'unrepairable',
      'Wont Fix'      => 'wont_fix',
      'duplicate'     => 'duplicate',
      'Approved'      => 'approved',
      'Not Approved'  => 'not_approved',
      'All'           => 'all',
      'Active'        => 'active',
      'Closed'        => 'closed',
      'Invalid'       => 'invalid'
    }
  end 
  
end
